﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using SWS;

public class RoadNode : MonoBehaviour
{
    /// <summary>
    /// 下个路径节点数组
    /// </summary>
    public RoadNode[] NextNodes;

    public PathManager[] NextNodeCurve;

    /// <summary>
    /// 本身的路径
    /// </summary>
    public PathManager SelfPath;

    /// <summary>
    /// 储存当前车辆的路程消耗信息
    /// </summary>
    public Dictionary<int, CostInfomation> CostInfoDic;

    private void Awake()
    {
        SelfPath = GetComponent<PathManager>();
        CostInfoDic = new Dictionary<int, CostInfomation>();
    }

    /// <summary>
    /// 计算当前路径预计消耗的距离，并保存到对应id的字典中储存
    /// </summary>
    /// <param name="carIndex">车辆id</param>
    /// <param name="startPos">起点</param>
    /// <param name="endPos">终点</param>
    /// <param name="parentNode">父路径节点</param>
    /// <param name="parentPathCurve">父路径弯道</param>
    public void CalculateLength(int carIndex,Vector3 startPos,Vector3 endPos,RoadNode parentNode,PathManager parentPathCurve)
    {
        Vector3 pathStartPos;
        Vector3 pathEndPos;
        if (SelfPath as BezierPathManager)
        {
            BezierPathManager B_SelfPath = SelfPath as BezierPathManager;
            pathStartPos = B_SelfPath.pathPoints[0];
            pathEndPos = B_SelfPath.pathPoints.LastOrDefault();
        }
        else
        {
            pathStartPos = SelfPath.waypoints[0].position;
            pathEndPos = SelfPath.waypoints[SelfPath.waypoints.Length - 1].position;
        }
        CostInfomation info = new CostInfomation();
        info.SaveCostInfo(Vector3.Distance(startPos, pathStartPos), Vector3.Distance(endPos, pathEndPos), parentNode, parentPathCurve);
        if (!CostInfoDic.ContainsKey(carIndex))
        {
            //如果此车辆是新车辆,将距离保存到新建信息中
            CostInfoDic.Add(carIndex,info);
        }
        else
        {
            CostInfoDic[carIndex] = info;
        }
    }

    
}
